package com.cans.lightning.business.lowcode.dao;

import com.cans.lightning.business.lowcode.dao.api.IAppFormDataDao;
import com.cans.lightning.business.lowcode.enums.AppFormSysField;
import com.google.common.collect.Lists;
import org.apache.logging.log4j.util.Strings;
import org.beetl.sql.core.SQLManager;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author cans
 * @date 2023/11/25
 **/
@Repository("appFormDataDao")
public class AppFormDataDaoImpl implements IAppFormDataDao {

    @Resource
    private SQLManager sqlManager;

    @Override
    public List<Map> selectDataList(String tableName) {
        return sqlManager.execute("SELECT * FROM " + tableName + " order by " + AppFormSysField.CREATE_TIME + " desc", Map.class, null);
    }

    @Override
    public void deleteFormData(String dbTableName, Map<String, String> param) {
        List<String> idINList = Lists.newArrayList();
        for (String id : param.keySet()) {
            idINList.add(String.format("#{%s}", id));
        }
        String join = Strings.join(idINList, ',');
        sqlManager.executeUpdate("DELETE FROM " + dbTableName + " WHERE id in ( " + join + " )", param);
    }
}
